<template>
  <div class="container">
    <div class="content" style="margin-top:30px;" v-show="this.$local.isExist('key')">
      <!-- <div class="header">购物车</div> -->
      <div class="pay" v-show="isNull()">
        <div class="left">

          <div class="info">
            <div><input type="checkbox" v-model="isAll"></div>
            <div><span>商品信息</span></div>
            <div><span>价格</span></div>
            <div><span>数量</span></div>
            <div><span>合计</span></div>
            <div><span>操作</span></div>
          </div>
          <div style="clear:both;"></div>

          <div class="item" v-for="item in carList" :key="item.car_id">
            <div><input type="checkbox" :checked="item.car_goods_state" 
                  v-model="item.car_goods_state"></div>
            <div><img v-lazy="item.goods_url" alt="出错了"></div>
            <div style="overflow:hidden;">
              <span style="text-align:left;color:gray;font-size:14px;">{{item.goods_title}}</span></div>
            <div><span style="color:red;">￥{{item.goods_price}}</span></div>
            <div><span span style="color:red;">{{item.car_goods_num}}</span></div>
            <div><span style="color:red;">￥{{item.car_goods_num*item.goods_price}}</span></div>
            <div><span @click="deleteCar(item.car_id)"><i class="el-icon-delete"></i></span></div>
          </div>

        </div>

        <div class="right">
          <div>购物车</div>
          <div style="text-align:left;">
            <p>操作</p>
            <input type="checkbox" v-model="isAll"><span style="font-size:14px;">全选</span>
            <button @click="deleteSelected">删除选中的商品</button>
            <button @click="emptyCarr">清空购物车</button>
          </div>
          <div style="border-top:1px dashed #C69C6C;border-bottom:1px dashed #C69C6C;">
            <p>结算</p>
            <div class="text">
              <div><span>全部商品(件)</span></div>
              <div><span>已选择商品(件)</span></div>
              <div><span>总计</span></div>
            </div>
            <div class="text" id="text">
              <div><span>{{carList.length}}</span></div>
              <div><span>{{selectedAll}}</span></div>
              <div><span style="font-size:24px;">￥{{selectedPrice}}</span></div>
            </div>
            <div style="clear:both;"></div>
          </div>
          
          <div @click="goSettlement" style="cursor:pointer;background:#E64347;color:white;margin-top:10px;">
            去结算
          </div>
          <div @click="goShoping" style="cursor:pointer;background:#C69C6C;color:white;margin-top:10px;">
            去购物
          </div>
        </div>

        <div style="clear:both;"></div>
      </div>
      <!-- 购物车无商品时显示 -->
      <div class="content" style="margin-top:30px;" v-show="!isNull()">
        <el-empty description="购物车为空！">
          <button @click="addCar">去添加</button>
        </el-empty>
      </div>
    </div>
    <!-- 未登录时显示！ -->
    <div class="content" style="margin-top:30px;" v-show="!this.$local.isExist('key')">
      <el-empty description="用户未登录，请点击登录按钮登录！">
        <button @click="goLogin">登录</button>
      </el-empty>
    </div>
    
  </div>
</template>

<script>
import {reqCarList,reqCarDelete,reqCarDeleteSeletced} from '../api/index'
import {mapState} from 'vuex'
export default {
  name:'Car',
  mounted() {
    if(this.$local.isExist('key')){
      this.$store.dispatch('carList',reqCarList(this.$local.get('key')));
    } 
  },
  methods: {
    isNull(){ //购物车无商品时显示
      if(this.$store.state.carList.length === 0){
        return false
      }else{
        return true
      }
    },
    addCar(){ //去添加到购物车按钮
      this.$router.push('/mall')
    },
    goShoping(){ //去购物按钮
      this.$router.push('/index')
    },
    goSettlement(){ //去结算按钮
      let arr = []
      this.carList.forEach(item => {
        if (item.car_goods_state)  arr.push({car_id:item.car_id,goods_id:item.goods_id,goods_title:item.goods_title,goods_url:item.goods_url,goods_price:item.goods_price,car_goods_num:item.car_goods_num})
      });
      const array = JSON.stringify(arr)
      this.$router.push({path:'/settlement',query:{value:array}})
    },
    goLogin(){ //前往登录界面
      this.$router.push('/user/info')
    },
    async deleteCar(id){ //删除购物车单个商品
      await reqCarDelete(id)
      if(this.$local.isExist('key')){
        this.$store.dispatch('carList',reqCarList(this.$local.get('key')));
      } 
      this.$message.success('删除成功！')
    },
    async deleteSelected(){ //删除选中得商品
      let arr = []
      this.carList.forEach(item => {
        if(item.car_goods_state) arr.push(item.car_id)
      });
      await reqCarDeleteSeletced(arr)
      if(this.$local.isExist('key')){
        this.$store.dispatch('carList',reqCarList(this.$local.get('key')));
      } 
      this.$message.success('选中商品删除成功！')
    },
    async emptyCarr(){ //清空购物车
      let arr = []
      this.carList.forEach(item => {
        arr.push(item.car_id)
      });
      await reqCarDeleteSeletced(arr)
      if(this.$local.isExist('key')){
        this.$store.dispatch('carList',reqCarList(this.$local.get('key')));
      } 
      this.$message.success('清空购物车成功！')
    }
  },
  computed:{
    // 映射vuex仓库中购物车的数据
    ...mapState(['carList']),
    selectedAll(){ //选中的总数
      return this.carList.reduce((pre,cur) => pre + (cur.car_goods_state ? 1:0),0)
    },
    isAll:{ //是否全选
      get(){
        return this.carList.length === this.selectedAll && this.carList.length > 0
      },
      set(value){
				return this.carList.forEach(item => {
          item.car_goods_state = value
        })
			}
    },
    selectedPrice(){ //已选择商品的总价
      let total = 0
      this.carList.forEach(item => {
        if (item.car_goods_state === true) total += item.goods_price*item.car_goods_num
      })
      return total
    }
  }
}
</script>

<style scoped>
  .header{
    background: white;
    margin-top: 10px;
    margin-bottom: 10px;
    font-size: 26px;
    font-family: '微软雅黑';
    color: #C69C6C;
    border-radius: 8px;
    padding: 10px 0 10px 10px;
  }
  .pay{
    position: relative;
    text-align: center;
  }
  .left,.right{
    float: left;
    padding: 20px 10px 10px 10px;
    background: white;
    border-radius: 8px;
  }
  .left{
    width: 622px;
    height: 420px;
    overflow: auto;
    margin-right: 16px;
  }
  .right{
    width: 330px;
    height: 420px;
  }
  .right > div{
    width: 80%;
    margin: 0 auto;
    border-radius: 4px;
    padding-top:8px;
    padding-bottom:8px;
  }
  .right p{
    text-align: left;
    font-size: 14px;
    color: gray;
    line-height: 40px;
  }
  .right button{
    margin-left: 13.5px;
  }
  .text{
    width: 50%;
    float: left;
    text-align: left;
    font-size: 14px;
    color: gray;
    line-height: 40px;
  }
  #text{
    text-align: right;
    font-family: '微软雅黑';
    color: red;
  }
  .info div{
    float: left;
    margin-right: 2%;
  }
  .info div:nth-of-type(2){
    width: 38%;
  }
  .info div:nth-of-type(3),.info div:nth-of-type(5){
    width: 10%;
  }
  .item{
    overflow: hidden;
    margin-top: 10px;
    border-bottom: 0.1px solid rgba(128, 128, 128, 0.26);
  }
  .item div{
    float: left;
    margin-right: 2%;
  }
  .item div:nth-of-type(2){
    width: 12%;
  }
  .item div:nth-of-type(3){
    width: 24%;
  }
  .item div:nth-of-type(5),.info div:nth-of-type(4){
    width: 22%;
  }
  .item div:nth-of-type(4),.item div:nth-of-type(6){
    width: 10%;
  }
  .item div:nth-of-type(7){
    margin-right: 0;
    width: 6%;
  }
  .left span{
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  input{
    cursor: pointer;
  }
</style>